/**
 * @file j1939_api.h
 * @author Jomb (jomb.liu@solinftec.com)
 * @brief
 * @version 0.1
 * @date 2022-06-08
 *
 * @copyright Copyright (c) 2022
 *
 */
#ifndef J1939_API_H
#define J1939_API_H

#ifndef NULL
#define NULL ((void *)0)
#endif

#define J1939_SUCCESS 0

// MSG functions
// not finished, the strings have to be placed to the flash and printed out
// using the printf_P function
/// Definition of MSG_ERR
// ---------------------
#ifdef DEBUG_ERR_CONSOLE_ON
#define LOG_E(TAG, num, str, val)
#else
#define LOG_E(num, str, val) // todo: user printf
#endif

/// Definition of MSG_WAR
// ---------------------
#ifdef DEBUG_WAR_CONSOLE_ON
#define LOG_D(num, str, val)
#else
#define LOG_D(num, str, val) // todo: user printf
#endif

typedef union
{
    unsigned int id;
    struct
    {
        unsigned char sa : 8;
        unsigned char ps : 8;
        unsigned char pf : 8;
        unsigned char dp : 1;
        unsigned char edp : 1;
        unsigned char property : 3;

    } un_id;
    /* data */
} un_can_exid;

typedef struct
{
    un_can_exid un_id;     /*!< can exid >*/
    unsigned char len;     /*!< can len >*/
    unsigned char data[8]; /*!< can data>*/
} j1939_message;

typedef long (*can_send_t)(j1939_message m);

#define J1939_READ_MUTEX 0
#define J1939_WRITE_MUTEX 1
typedef int (*j1939_mutex_take_t)(int flag);
typedef int (*j1939_mutex_release_t)(int flag);

typedef int (*j1939_spn_callback_t)(unsigned int pgn,
                                    unsigned int spn,
                                    unsigned int flags);

int j1939_init(char addr,
               can_send_t send,
               j1939_mutex_take_t take,
               j1939_mutex_release_t release);

int j1939_dispatch(j1939_message *m);

int j1939_write_net(unsigned int pgn,
                    unsigned char property);

int j1939_write_local(unsigned int pgn,
                      unsigned int spn,
                      void *data,
                      unsigned int size);

int j1939_read_local(unsigned int pgn,
                     unsigned int spn,
                     void *data,
                     unsigned int size);

int j1939_register_spn_cb(unsigned int pgn,
                          unsigned int spn,
                          j1939_spn_callback_t cb);

#endif
